package shuashuami.hb.com.avtivity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.CUntreadeterTaskAadpter;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpCMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.model.CUntreaterTask;
import shuashuami.hb.com.share.ShareCUserInfo;
import shuashuami.hb.com.util.LoadType;
import shuashuami.hb.com.view.pull.PullToRefreshLayout;

public class CCompleteTaskActivity extends AbActivity {
    private ListView listView;

    private CUntreadeterTaskAadpter adatper;
    private List<CUntreaterTask> mResources;

    private String type = "6";
    private int size = 10;
    private int maxpage=-1;
    private PullToRefreshLayout mptrl;
    private int page = 1;

    private LinearLayout llIsShwo;

    private EditText edit_next;

    private ImageView image_searchbtn;
    private boolean isSearch=false;

    private String keyword="";
    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_ccomplete_task);
        listView = (ListView) findViewById(R.id.lv_listview);
        mptrl= (PullToRefreshLayout) findViewById(R.id.refresh_view);
        llIsShwo = (LinearLayout) findViewById(R.id.ll_cuntreated_sishow);
        edit_next= (EditText) findViewById(R.id.edit_next);
        image_searchbtn= (ImageView) findViewById(R.id.image_searchbtn);
    }

    @Override
    protected void initData() {
        setLeftView();
        mResources=new ArrayList<>();
        adatper=new CUntreadeterTaskAadpter(mResources,this);
        listView.setAdapter(adatper);
        getMessage();
    }

    public int operateType = LoadType.NO_OPERATE;

    @Override
    protected void initListener() {
        mptrl.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {


            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                //
                operateType= LoadType.DO_REFERSH;
                page=1;
                getMessage();

            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                if(maxpage==-1 || page==maxpage){
                    mptrl.loadmoreFinish(PullToRefreshLayout.NODATA);
                    return;
                }
//
                operateType= LoadType.LOAD_MORE;
                page++;
                getMessage();
            }
        });

        edit_next.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            //            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    edit_next.bringToFront();
                    isSearch=false;
                }else{
                    image_searchbtn.bringToFront();
                    isSearch=true;
                }

            }
        });

        image_searchbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isSearch){
                    operateType= LoadType.DO_REFERSH;
                    edit_next.clearFocus();
                    keyword=edit_next.getText().toString();
                    page=1;
                    getMessage();
//                    mAllClickFragm.disPlayData(edit_next.getText().toString());
//                    mAuditClickFragm.disPlayData(edit_next.getText().toString());
                }
            }
        });
    }

    private void initAdapter( List<CUntreaterTask> list) {


        // 如果是刷新
        if(operateType== LoadType.DO_REFERSH){
            mResources.clear();
        }


        mResources.addAll(list);

        adatper.notifyDataSetChanged();
        if(operateType== LoadType.DO_REFERSH){
            mptrl.refreshFinish(PullToRefreshLayout.SUCCEED);
        }
        if(operateType== LoadType.LOAD_MORE){
            mptrl.loadmoreFinish(PullToRefreshLayout.SUCCEED);
        }

        operateType= LoadType.NO_OPERATE;

    }

//    private void initAdapter(JSONArray jsonArray) {
//
//        adatper = new CUntreadeterTaskAadpter(list, CCompleteTaskActivity.this);
//        listView.setAdapter(adatper);
//    }

    private String toHH(Long ms) {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");//初始化Formatter的转换格式。

        String hms = formatter.format(ms);
        return hms;
    }

    private void getMessage() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        HttpCMethods.getUnreadterTask(listener, shareCUserInfo.getId(), type,
                page + "", "10",keyword
        );
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        List<CUntreaterTask> list = new ArrayList<>();
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(CCompleteTaskActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject s = jsonObject.getJSONObject("content");
                            JSONArray jsonArray = s.getJSONArray("list");
                            page= s.getInt("currentpage");
                            maxpage=s.getInt("maxpage");
                            List<String> qq = new ArrayList<>();
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject1 = jsonArray.getJSONObject(i).getJSONObject("shop");
                                qq.add(jsonObject1.getString("qq"));
                            }


                            for (int i = 0; i < jsonArray.length(); i++) {
                                try {
                                    JSONObject temp = jsonArray.getJSONObject(i);
                                    CUntreaterTask cUntreaterTask = new CUntreaterTask(temp.getString("id"), temp.getString("platform_image"), temp.getString("goods_image"),
                                            temp.getString("task_sn"),
                                            temp.getString("status"), temp.getString("goods_title"),
                                            temp.getString("spot_money"), temp.getString("num"), temp.getString("price"),
                                            temp.getString("addtime"), temp.getString("shop_id"), qq.get(i), temp.getString("commission"));
                                    list.add(cUntreaterTask);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        } else {
                            llIsShwo.setVisibility(View.VISIBLE);
                        }
                        initAdapter(list);

                    } catch (JSONException e) {
                        ToastUtil.showShort(CCompleteTaskActivity.this, "数据解析错误");
                    }


                    break;
            }

            super.handleMessage(msg);
        }
    };
}
